import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
import routes from './routes';
//先把VueRouter原型对象的push/replace先保存一份
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
//重写push|replace方法
// 1.location传递的哪些参数，告诉原来的push，往哪里跳转 2.resolve:成功回调， 3.失败回调
VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve && reject) {
        //call||apply区别：相同点都可以调用函数，都可以篡改函数this上下文
        //不同点：传递参数的不同，call传递参数用逗号隔开，apply数组
        originPush.call(this, location, resolve, reject)
    } else {
        originPush.call(this, location, () => { }, () => { })
    }
};
VueRouter.prototype.place = function (location, resolve, reject) {
    if (resolve && reject) {
        //call||apply区别：相同点都可以调用函数，都可以篡改函数this上下文
        //不同点：传递参数的不同，call传递参数用逗号隔开，apply数组
        originReplace.call(this, location, resolve, reject)
    } else {
        originReplace.call(this, location, () => { }, () => { })
    }
};

let router = new VueRouter({
    routes,
    scrollBehavior(to, from, savedPosition) {
        //y等于零代表滚动条在最上方
        return { y: 0 }
    }
})
export default router;